<template>
  <div class="lists">
    <el-table
      :data="bannerListData"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
      height="510"
    >
      <el-table-column prop="id" label="编号" sortable width="180">
      </el-table-column>
      <el-table-column prop="title" label="名称" sortable width="180">
      </el-table-column>
      <el-table-column prop="img" label="图片">
        <template slot-scope="scope">
          <img
            v-show="scope.row.img != ''"
            :src="baseURL + scope.row.img"
            alt=""
            srcset=""
            class="goodsImg"
          />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 1">启用</el-tag>
          <el-tag v-if="scope.row.status == 2" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="editBanner(scope.row.id)"
            >编辑</el-button
          >
          <el-button type="danger" @click="deleteBanner(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getBannerList, getBannerDelete } from "../../../request/api";
export default {
  props: ["dialog"],
  data() {
    return {
      // 活动数据列表
      bannerListData: [],
    };
  },
  methods: {
    // 获取活动数据列表
    getBannerListData() {
      getBannerList().then(
        (res) => {
          if (res.data.code == 200) {
            this.bannerListData = res.data.list;
          }
        },
        (err) => err
      );
    },
    // 展示编辑对话框,并展示编辑信息
    editBanner(id) {
      this.$emit("editBanner", id);
    },
    // 删除
    deleteBanner(id) {
      this.$confirm("是否删除该活动?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      }).then(
        () => {
          getBannerDelete({ id }).then(
            (res) => {
              if (res.data.code == 200) {
                this.$message.success("删除成功");
                this.dialog.isShowDialog = false;
                this.getBannerListData();
              }
            },
            (err) => err
          );
        },
        (err) => err
      );
    },
  },
  mounted() {
    this.getBannerListData();
  },
};
</script>

<style scoped>
.goodsImg {
  width: 150px;
  height: 100px;
}
</style>